<?php
//员工控制器
class EmployeAction extends CommonAction {
    /**
     * [employeList description]员工列表
     * @return [type] [description]
     */
    public function employeList(){
        $this->getDepartment();
        $this->display('Employe:employeList');
    }


    /**
     * [leaveListData description]获取请假列表
     * @return [type] [description]
     */
    public function employeListData(){
        $employe_work_id=I('employe_work_id');
        $employe_name=I('employe_name');
        $employe_department_id=I('employe_department_id');
    	$result = array();
    	$result['total'] = $this->getListCount($employe_work_id,$employe_name,$employe_department_id);
    	$result['rows'] = $this->getList($employe_work_id,$employe_name,$employe_department_id);
    	echo json_encode($result);
    }

    /**
     * 获取列表
     */
    public function getList($employe_work_id,$employe_name,$employe_department_id){
    	import("ORG.Util.Page");
    	$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
    	$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
       	$employe=M('Employe');
        $result=array();
       	$data=$employe->where("employe_work_id like '%".$employe_work_id."%' and employe_name like '%".$employe_name."%' and employe_department_id like '%".$employe_department_id."%'")->page($page,$rows)->select();

        foreach ($data as $value) {
            $value['employe_department_id']=$this->getDepartmentName($value['employe_department_id']);
            $result[]=$value;
        }
        return $result;
    }

    /**
     * 获取列表数量
     */
    public function getListCount($employe_work_id,$employe_name,$employe_department_id){	
       
    	$employe=M('Employe');
    	$result=$employe->where("employe_work_id like '%".$employe_work_id."%' and employe_name like '%".$employe_name."%' and employe_department_id like '%".$employe_department_id."%'")->count();
        return $result;
    }

    /**
     * [delEmploye description]删除员工
     * @return [type] [description]
     */
    public function delEmploye(){
        $employe=M('Employe');
        $employe_id=$_POST['employe_id'];
        $result=$employe->where("employe_id ='".$employe_id."'")->delete();
        if($result){
            echo 'ok';
        }
        else{
            echo 'no';
        }
    }

    /**
     * [addEmploye description]跳转添加模版
     */
    public function addEmploye(){
        $this->getRole();
        $this->display("Employe:addEmploye");
    }

    /**
     * [addEmployePost description]处理添加
     */
    public function addEmployePost(){
        $employe=M('Employe');
        $employe->employe_work_id=strval(trim($_POST['employe_work_id']));
        $employe->employe_role=trim($_POST['employe_role']);
        $employe->employe_photo=$_POST['employe_photo'];
        $employe->employe_password=md5('123456');
        $employe->employe_name=strval(trim($_POST['employe_name']));
        $employe->employe_department_id=$_POST['employe_department_id'];
        $employe->employe_sex=$_POST['employe_sex'];
        $employe->employe_identity_card=$_POST['employe_identity_card'];
        $employe->employe_brithday=$_POST['employe_brithday'];
        $employe->employe_if_marray=$_POST['employe_if_marray'];
        $employe->employe_nation=$_POST['employe_nation'];
        $employe->employe_native=$_POST['employe_native'];
        $employe->employe_politics=$_POST['employe_politics'];
        $employe->employe_account_type=$_POST['employe_account_type'];
        $employe->employe_account_native=$_POST['employe_account_native'];
        $employe->employe_position=$_POST['employe_position'];
        $employe->employe_work_state=$_POST['employe_work_state'];
        $employe->employe_hiredate=$_POST['employe_hiredate'];
        $employe->employe_departure_date=$_POST['employe_departure_date'];
        $employe->employe_phone=$_POST['employe_phone'];
        $employe->employe_qq=$_POST['employe_qq'];
        $employe->employe_email=$_POST['employe_email'];
        $employe->employe_family_phone=$_POST['employe_family_phone'];
        $employe->employe_family_address=$_POST['employe_family_address'];
        $employe->employe_graduate_school=$_POST['employe_graduate_school'];
        $employe->employe_graduate_time=$_POST['employe_graduate_time'];
        $employe->employe_degree=$_POST['employe_degree'];
        $employe->employe_specialty=$_POST['employe_specialty'];
        $employe->employe_social_relations=$_POST['employe_social_relations'];
        $employe->employe_work_exerience=$_POST['employe_work_exerience'];
        $employe->employe_work_skill=$_POST['employe_work_skill'];
        $employe->employe_remark=$_POST['employe_remark'];
        $employe->employe_other='123456';
        $employe->employe_file=$_POST['employe_file'];
        if($employe_id=$employe->add()){
            echo 'ok';
        }
        else{
            echo 'no';
        }
    }

    /**
     * [editEmploye description]编辑员工信息
     * @return [type] [description]
     */
    public function editEmploye(){
        $employe_id=$_GET['employe_id'];
        $employe=M("Employe");
        $this->employeData=$employe->where("employe_id='".$employe_id."'")->select();

        $this->getRole();

        $this->getDepartment();
        
        $this->display("Employe:editEmploye");
    }

    /**
     * [editEmployePost description]编辑处理
     * @return [type] [description]
     */
    public function editEmployePost(){
        $employe=M('Employe');
        $employe_id=$_POST['employe_id'];
        $data['employe_role']=$_POST['employe_role'];
        $data['employe_photo']=$_POST['employe_photo'];
        $data['employe_name']=$_POST['employe_name'];
        $data['employe_department_id']=$_POST['employe_department_id'];
        $data['employe_sex']=$_POST['employe_sex'];
        $data['employe_identity_card']=$_POST['employe_identity_card'];
        $data['employe_brithday']=$_POST['employe_brithday'];
        $data['employe_if_marray']=$_POST['employe_if_marray'];
        $data['employe_nation']=$_POST['employe_nation'];
        $data['employe_politics']=$_POST['employe_politics'];
        $data['employe_account_type']=$_POST['employe_account_type'];
        $data['employe_account_native']=$_POST['employe_account_native'];
        $data['employe_position']=$_POST['employe_position'];
        $data['employe_work_state']=$_POST['employe_work_state'];
        $data['employe_hiredate']=$_POST['employe_hiredate'];
        $data['employe_departure_date']=$_POST['employe_departure_date'];
        $data['employe_phone']=$_POST['employe_phone'];
        $data['employe_qq']=$_POST['employe_qq'];
        $data['employe_email']=$_POST['employe_email'];
        $data['employe_family_phone']=$_POST['employe_family_phone'];
        $data['employe_family_address']=$_POST['employe_family_address'];
        $data['employe_graduate_school']=$_POST['employe_graduate_school'];
        $data['employe_graduate_time']=$_POST['employe_graduate_time'];
        $data['employe_degree']=$_POST['employe_degree'];
        $data['employe_specialty']=$_POST['employe_specialty'];
        $data['employe_social_relations']=$_POST['employe_social_relations'];
        $data['employe_work_exerience']=$_POST['employe_work_exerience'];
        $data['employe_work_skill']=$_POST['employe_work_skill'];
        $data['employe_remark']=$_POST['employe_remark'];
        $data['employe_file']=$_POST['employe_file'];
        $result=$employe->where("employe_id='".$employe_id."'")->save($data);
        if($result!=false){
            echo 'ok';
        }
        else{
            echo 'no';
        }
    }

    /**
     * [check_employe_work_id description]判断工号是否被注册
     * @return [type] [description]
     */
    public function check_employe_work_id(){
        $employe_work_id=$_POST['employe_work_id'];
        $employe=M('Employe');
        $result=$employe->where("employe_work_id ='".$employe_work_id."'")->find();
        if($result)
        {
            echo 'no';
        }
        else
        {   
            echo 'ok';
        }
    }

    /**
     * [selectEmploye description]查看员工信息
     * @return [type] [description]
     */
    public function selectEmploye(){
        $employe_id=$_GET['employe_id'];
        $employe=M("Employe");
        $this->employeData=$employe->where("employe_id='".$employe_id."'")->select();
        $this->display("Employe:selectEmploye");
    }

    /**
     * [UserList description]用户管理
     */
    public function UserList(){
    	$data=$this->getAllDepartment();
        $tree=$this->createTreeStuctDpt($data);
        $json=$this->createSelectHtml($tree);
        $this->assign('json',$json);
    	$this->display("Employe:userList");
    }

    /**
     * [changePassword description]修改密码
     * @return [type] [description]
     */
    public function changePassword(){
    	$employe_password=I('employePassword');
    	$employe_id=I('employe_id');
    	$employe=M('Employe');
    	$data['employe_password']=md5($employe_password);
    	$data['employe_other']=$employe_password;
    	$result=$employe->where("employe_id='".$employe_id."'")->save($data);
    	if($result){
            echo 'ok';
        }
        else{
            echo 'no';
        }
    }

}

